package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 查询当前用户所有端地址信息
     * @param addressBook
     * @return
     */
    @Override
    public List<AddressBook> list(AddressBook addressBook) {
        List<AddressBook> list= addressBookMapper.list(addressBook);

        return list;
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.inster(addressBook);
    }

    /**
     * 根据id查询地址
     * @return
     */
    @Override
    public AddressBook getById(Long id) {

        AddressBook list = addressBookMapper.selectById(id);
        return list;
    }
    /**
     * 根据id修改地址
     * @return
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 设置默认地址
     * @return
     */
    @Override
    @Transactional
    public void setDefault(AddressBook addressBook) {

        Long id = addressBook.getId();
        addressBookMapper.setIsDefault(id);
        addressBookMapper.updateDefault(id);
    }

    /**
     * 根据id删除地址
     * @return
     */
    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }
}
